package com.study.talk.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import android.widget.TextView
import com.bumptech.glide.Glide
import com.google.android.material.imageview.ShapeableImageView
import com.study.talk.R
import com.study.talk.data.model.GroupMessageRecord
import com.study.talk.data.model.MessageRecord
import com.study.talk.di.AppModule
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.time.delay
import kotlinx.coroutines.withContext
import java.time.Duration

object PopWindowUtil {
    @SuppressLint("InflateParams")
    fun showPopWindow(context: Activity, view: View, message: MessageRecord, call: () -> Unit) {
        val contentView = LayoutInflater.from(context).inflate(R.layout.pop_layout, null)
        val popWindowImageView = contentView.findViewById<ShapeableImageView>(R.id.pop_window_image_view)
        val popWindowTextView = contentView.findViewById<TextView>(R.id.pop_window_text_view)
        popWindowTextView.text = if (message.messageType == 0) message.content else "[ 图片 ]"
        Glide.with(context).load(AppModule.getWebUrl() + message.avatar).into(popWindowImageView)
        val popupWindow = PopupWindow(context).apply {
            height = ViewGroup.LayoutParams.MATCH_PARENT
            width = ViewGroup.LayoutParams.MATCH_PARENT
            isOutsideTouchable = true
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            isFocusable = true
            animationStyle = R.style.popStyle
            isTouchable = true
            setContentView(contentView)
            showAtLocation(view, Gravity.TOP, 0, 0)
        }
        contentView.setOnClickListener { _ ->
            call.invoke()
            popupWindow.dismiss()
        }
        CoroutineScope(Dispatchers.IO).launch{
            delay(Duration.ofSeconds(5))
            withContext(Dispatchers.Main){
                if (popupWindow.isShowing) {
                    popupWindow.dismiss()
                }
            }
        }
    }


    @SuppressLint("InflateParams")
    fun showPopWindow(context: Activity, view: View, message: GroupMessageRecord, call: () -> Unit) {
        val contentView = LayoutInflater.from(context).inflate(R.layout.pop_layout, null)
        val popWindowImageView = contentView.findViewById<ShapeableImageView>(R.id.pop_window_image_view)
        val popWindowTextView = contentView.findViewById<TextView>(R.id.pop_window_text_view)
        popWindowTextView.text = if (message.messageType == 1) "[ 图片 ]" else message.content
        Glide.with(context).load(AppModule.getWebUrl() + message.avatar).into(popWindowImageView)
        val popupWindow = PopupWindow(context).apply {
            height = ViewGroup.LayoutParams.MATCH_PARENT
            width = ViewGroup.LayoutParams.MATCH_PARENT
            isOutsideTouchable = true
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            isFocusable = true
            animationStyle = R.style.popStyle
            isTouchable = true
            setContentView(contentView)
            showAtLocation(view, Gravity.TOP, 0, 0)
        }
        contentView.setOnClickListener { _ ->
            call.invoke()
            popupWindow.dismiss()
        }
        CoroutineScope(Dispatchers.IO).launch{
            delay(Duration.ofSeconds(5))
            withContext(Dispatchers.Main){
                if (popupWindow.isShowing) {
                    popupWindow.dismiss()
                }
            }
        }
    }
}